package com.wooophone.webapp.admin.web.action;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.wooophone.mpyeah.biz.RealStoreBiz;
import com.wooophone.mpyeah.entity.RealStore;
import com.wooophone.webapp.admin.web.util.FileUtil;
import com.wooophone.common.web.action.BaseAction;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;

public class RealStoreAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private RealStore realStore;
	private RealStoreBiz realStoreBiz;
	private String keyword;
	private List<RealStore> storeList;
	private int level;
	private String brandId;
	private File upload;
	private String uploadFileName;
	private String filePath;
	private String inFilePath;
	
	private File uploadScene;
	private String uploadSceneFileName;

	public String list() {
		try {
			if (brandId != null && !"".equals(brandId)) {
				Map<String, Object> parameter = new HashMap<String, Object>();
				parameter.put("brandId", brandId);
				pager.setRecordCount(realStoreBiz.count(parameter));
				storeList = realStoreBiz.find(parameter,
						(pager.getCurrPageNo() - 1) * pager.getPageSize(),
						pager.getPageSize());
			} else {
				pager.setRecordCount(realStoreBiz.count());
				storeList = realStoreBiz.list((pager.getCurrPageNo() - 1)
						* pager.getPageSize(), pager.getPageSize());
			}
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		

	}

	public String save() {
//		if (upload != null) {
//			String imageFileName = null;
//			imageFileName = System.currentTimeMillis()
//					+ uploadFileName.substring(uploadFileName.lastIndexOf("."));
//			File imageFile = new File(ServletActionContext.getServletContext()
//					.getRealPath("/upload/image") + "/" + imageFileName);
//			FileUtil.copy(upload, imageFile);
//			realStore.setShopFrontImg("upload/image" + "/"
//					+ imageFileName);
//		}
//
//		if (uploadScene != null) {
//			String imageFileName = null;
//			imageFileName = System.currentTimeMillis()
//					+ uploadSceneFileName.substring(uploadSceneFileName
//							.lastIndexOf("."));
//			File imageFile = new File(ServletActionContext.getServletContext()
//					.getRealPath("/upload/image") + "/" + imageFileName);
//			FileUtil.copy(upload, imageFile);
//			realStore.setIndoorSceneImg("upload/image" + "/"
//					+ imageFileName);
//		}
		
		realStore.setShopFrontImg(filePath);
		realStore.setIndoorSceneImg(inFilePath);
		realStore.setBrandId(brandId);
		realStore.setStatus(1);
		realStore.setCreator(ServletActionContext.getRequest().getUserPrincipal().getName());
		try {
			realStoreBiz.save(realStore);
		} catch (Exception e) {
			e.printStackTrace();
			return error(getText("realStore.save.exception"));
		}
		this.sendUrl="realStore_list.action?brandId="+brandId;
		Map<String, Object> result = new HashMap<String, Object>();
		if (realStore != null && realStore.getId() != null) {
			if (AJAX.equals(type)) {
				result.put("status", "success");
				return ajax(result);
			} else {
				return success(getText("realStore.save.success"));
			}
		} else {
			if (AJAX.equals(type)) {
				result.put("status", "error");
				result.put("message", getText("realStore.save.error"));
				return ajax(result);
			} else {
				return error(getText("realStore.save.error"));
			}

		}

	}

	@RequiredStringValidator(fieldName = "id", key = "noid", message = "no id")
	public String delete() {
		try {
			int r = realStoreBiz.delete(id);
			if (r > 0) {
				if (AJAX.equals(type)) {
					return ajax(Status.success);
				} else {
					return SUCCESS;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (AJAX.equals(type)) {
			return ajax(Status.error);
		} else {
			return ERROR;
		}
	}

	public String update() {
		RealStore realStoreOld = null;
		if (realStore != null && realStore.getId() != null
				&& !"".equals(realStore.getId())) {
			realStoreOld = realStoreBiz.get(realStore.getId());
		}
		if (brandId != null && !"".equals(brandId)) {
			realStoreOld.setBrandId(brandId);
		}
		realStoreOld.setDescription(realStore.getDescription());
		realStoreOld.setGrade(realStore.getGrade());
		realStoreOld.setMore(realStore.getMore());
		realStoreOld.setName(realStore.getName());
		if(filePath!=null&&!"".equals(filePath)){
			realStoreOld.setShopFrontImg(filePath);
		}
		if(inFilePath!=null&&!"".equals(inFilePath)){
			realStoreOld.setIndoorSceneImg(inFilePath);
		}
		try {
//			if (upload != null) {
//				String imageFileName = null;
//				imageFileName = System.currentTimeMillis()
//						+ uploadFileName.substring(uploadFileName
//								.lastIndexOf("."));
//				File imageFile = new File(ServletActionContext
//						.getServletContext().getRealPath("/upload/image")
//						+ "/" + imageFileName);
//
//				String oldFileName = realStoreOld.getShopFrontImg();
//				if (oldFileName != null && !"".equals(oldFileName)) {
//					oldFileName = oldFileName.substring(oldFileName
//							.lastIndexOf("/"));
//					File fileOld = new File(ServletActionContext
//							.getServletContext().getRealPath(
//									"/upload/image")
//							+ "/" + oldFileName);
//
//					if (fileOld.exists()) {
//						fileOld.delete();
//					}
//					
//				}
//				realStoreOld.setShopFrontImg("upload/image" + "/"
//						+ imageFileName);
//				FileUtil.copy(upload, imageFile);
//			}

//			if (uploadScene != null) {
//				String imageFileName = null;
//				imageFileName = System.currentTimeMillis()
//						+ uploadSceneFileName.substring(uploadSceneFileName
//								.lastIndexOf("."));
//				File imageFile = new File(ServletActionContext
//						.getServletContext().getRealPath("/upload/image")
//						+ "/" + imageFileName);
//				String oldFileName = realStoreOld.getIndoorSceneImg();
//				if (oldFileName != null && !"".equals(oldFileName)) {
//					oldFileName = oldFileName.substring(oldFileName
//							.lastIndexOf("/"));
//					File fileOld = new File(ServletActionContext
//							.getServletContext().getRealPath(
//									"/upload/image")
//							+ "/" + oldFileName);
//
//					if (fileOld.exists()) {
//						fileOld.delete();
//					}
//					
//				}
//				realStoreOld.setIndoorSceneImg("upload/image" + "/"
//						+ imageFileName);
//				FileUtil.copy(uploadScene, imageFile);
//
//			}

			int r = realStoreBiz.update(realStoreOld);
			if (r > 0) {
				return success(getText("realStore.update.success"));
			} else {
				return error(getText("realStore.update.error"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR_PAGE;
		}

	}

	@RequiredStringValidator(fieldName = "id", key = "noid", message = "no id")
	public String view() {
		realStore = realStoreBiz.get(id);
		return SUCCESS;
	}

	public String input() {
		return SUCCESS;

	}

	@RequiredStringValidator(fieldName = "id", key = "noid", message = "no id")
	public String edit() {
		try {
			realStore = realStoreBiz.get(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public String search() {
		try {
			if (null == keyword && "".equals(keyword)) {
				return list();
			} else {
				String keywordString = keyword;
				pager.setRecordCount(realStoreBiz.count(keywordString));
				storeList = realStoreBiz.search(keywordString,
						(pager.getCurrPageNo() - 1) * pager.getPageSize(),
						pager.getPageSize());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;

	}

	@RequiredStringValidator(fieldName = "id", key = "noid", message = "no id")
	public String commend() {
		try {
			realStore = realStoreBiz.get(id);
			realStore.setLevel(level);
			realStoreBiz.update(realStore);
			return ajax(Status.success);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return ajax(Status.error);
	}

	public String deleteSelect() {
		// try {
		// int r = 0;
		// for (int i = 0; i < .length; i++) {
		// r = brandBiz.delete(brandIdList[i]);
		// }
		// if (r > 0) {
		// if (AJAX.equals(type)) {
		// Map<String, Object> result = new HashMap<String, Object>();
		// result.put("status", "success");
		// result.put("count", r);
		// return ajax(result);
		// } else {
		// return SUCCESS;
		// }
		// }
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
		// if (AJAX.equals(type)) {
		// return ajax(Status.error);
		// } else {
		// return ERROR;
		// }
		return "";
	}
	
	
	public String test(){
		System.out.println("我来了");
		return ajax(Status.success);
	}

	public RealStore getRealStore() {
		return realStore;
	}

	public void setRealStore(RealStore realStore) {
		this.realStore = realStore;
	}

	public List<RealStore> getStoreList() {
		return storeList;
	}

	public void setRealStoreBiz(RealStoreBiz realStoreBiz) {
		this.realStoreBiz = realStoreBiz;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getBrandId() {
		return brandId;
	}

	public void setBrandId(String brandId) {
		this.brandId = brandId;
	}

	public void setUploadScene(File uploadScene) {
		this.uploadScene = uploadScene;
	}

	public void setUploadSceneFileName(String uploadSceneFileName) {
		this.uploadSceneFileName = uploadSceneFileName;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public String getInFilePath() {
		return inFilePath;
	}

	public void setInFilePath(String inFilePath) {
		this.inFilePath = inFilePath;
	}
	
	

}